﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListCompareDemo.Models
{
    public partial class DataGridSource<T> : ObservableObject
    {
        [ObservableProperty]
        int selectedIndex = -1;

        public ObservableCollection<T> Collection { get; } = [];

        public T SelectedItem
        {
            get
            {
                if (SelectedIndex == -1 || Collection.Count == 0)
                {
                    return default;
                }

                return Collection[SelectedIndex];
            }
        }

        public void Update(List<T> update)
        {
            try
            {
                var index = SelectedIndex;
                for (var i = 0; i < Collection.Count && i < update.Count; i++)
                {
                    Collection[i] = update[i];
                }
                for (var i = Collection.Count; i < update.Count; i++)
                {
                    Collection.Add(update[i]);
                }
                for (var i = Collection.Count - 1; i >= update.Count; i--)
                {
                    Collection.RemoveAt(i);
                }

                if (index >= Collection.Count)
                {

                    SelectedIndex = Collection.Count - 1;
                }
                else
                {
                    SelectedIndex = index;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }
        }

        /// <summary>
        /// DataGrid中的多选项
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        public void SelectRange(int startIndex, int endIndex)
        {
            try
            {
                var minIndex = System.Math.Min(startIndex, endIndex);
                var maxIndex = System.Math.Max(startIndex, endIndex);
                for (int i = minIndex; i <= maxIndex; i++)
                {
                    SelectedItems.Add(Collection[i]);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary>
        /// DataGrid中的多选项
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        public void AddsSingleRange(int Index)
        {
            try
            {
                SelectedItems.Add(Collection[Index]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary>
        /// DataGrid中的多选项
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        public void AddMultiSelectedItems()
        {
            SelectedItems.Clear();
        }

        /// <summary>
        /// DataGrid中的多选项
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        public void ClearMultiSelectedItems()
        {
            SelectedItems.Clear();
        }


        public ObservableCollection<T> SelectedItems { get; } = [];
    }
}
